package com.manager.service.impl;

import com.manager.dao.StoreMapper;
import com.manager.dao.StoreRegistrationMapper;
import com.manager.entity.Store;
import com.manager.entity.StoreRegistration;
import com.manager.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    public StoreMapper storeMapper;
    @Autowired
    public StoreRegistrationMapper storeRegistrationMapper;


    @Override
    public String register(String userid,String storeid, String description) {
        System.out.println(userid+storeid+description);
        Store store=new Store(userid,storeid,description);
        StoreRegistration storeRegistration=new StoreRegistration(storeid);
        int i=0,j=0;
        try {
            try {
                i=storeMapper.insert(store);
            } catch (org.springframework.dao.DuplicateKeyException e){
                e.printStackTrace();
                return "DuplicateKeyException:storeMapper.insert(store)";
                /*Caused by: java.sql.SQLIntegrityConstraintViolationException*/

            }catch (DataIntegrityViolationException e) {
                e.printStackTrace();
                return "DataIntegrityViolationException:storeMapper.insert(store)";
            } catch (Exception e) {
                e.printStackTrace();
                return "Exception";
            }

            j=storeRegistrationMapper.insert(storeRegistration);
            /*org.springframework.dao.DataIntegrityViolationException*/
            /*Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'registertime' cannot be null*/
        } catch (Exception e) {
            e.printStackTrace();
            return "Exception";
        }

        return "success:"+i+"&"+j;
    }


    /*把判断是否存在对应商店的代码也放在这里，这就可以根据userid获取商店id或者使得按钮变为注册。*/
    @Override
    public String getStoreidByUserid(String userid) {
        try{
            String string=storeMapper.selectByPrimaryKey(userid).getStoreid();
//            java.lang.NullPointerException
//            还是这个问题.? (2019-12-31 20:18:20)
            return string;
        }catch (java.lang.NullPointerException e){
            e.printStackTrace();
            return "null";
        }
    }

    /*这里添加一个捕获空指针异常表示我们要使得返回空值。【测试：（成功输出空。）】*/
    @Override
    public List<Store> getallstore() {
        /*Mapper层获取所有商店。*/
        return storeMapper.getallstore();
    }
}
